/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


/**
 *
 * @author Administrator
 */
public class CustomerManager {
    public boolean addCustomer(Customer customer) {
		return customers.add(customer);
	}
	
    public boolean delCustomer(Customer customer) {
            Customer temp;		

            for(int i = 0; i < customers.size(); i++) {
                    temp = customers.get(i);
                    if(temp == customer) {
                            customers.remove(i);
                            return true;
                    }
            }

            return false;
    }

    public void clearCustomer() {
            customers.clear();
    }

    public Customer getCustomerByID(String id) {
            Customer temp;		

            for(int i = 0; i < customers.size(); i++) {
                    temp = customers.get(i);
                    if(temp.getId().equals(id)) {				
                            return temp;
                    }
            }

            return null;
    }

    public boolean saveToFile(String path) {
            FileOutputStream fos = null; 
            ObjectOutputStream oos = null;
            boolean retCode = false;
            try{
                    fos = new FileOutputStream(path); 
                    oos = new ObjectOutputStream(fos);
                    for(int i = 0; i < customers.size(); ++i)
                            oos.writeObject(customers.get(i));
                    retCode = true;
            }catch(Exception e){
                    System.out.println("Exception during writing file:"+e.toString());
            }

            try {
                    if(oos != null)
                            oos.close();
                    if(fos != null)
                            fos.close();
            } catch (IOException e) {
                    System.out.println("Exception during closing file:"+e.toString());
            }

            return retCode;
    }

    public boolean readFromFile(String path) {
            FileInputStream fis = null; 
            ObjectInputStream ois = null;
            try{
                    fis = new FileInputStream(path); 
                    ois = new ObjectInputStream(fis);

                    //delete old ones
                    customers.clear();
                    while(true) {
                            Customer temp = (Customer) ois.readObject();		
                            customers.add(temp);
                    }

            }catch(EOFException e){
                    //end of file
                    try {
                            if(ois != null)
                                    ois.close();
                            if(fis != null)
                                    fis.close();
                    } catch (IOException ex) {
                            System.out.println("Exception during closing file:"+ex.toString());
                    }
            }catch(Exception e){
                    System.out.println("Exception during serialization:"+e.toString());
                    return false;
            }
            return true;
    }

    //Singleton
    public static CustomerManager getInstance() {
            if(_instance == null)
                    _instance = new CustomerManager();
            return _instance;
    }	

    private static CustomerManager _instance = null;


    //Getter & Setter
    public ArrayList<Customer> getCustomers() {
            return customers;
    }

    public void setUsers(ArrayList<Customer> customers) {
            this.customers = customers;
    }

    public Customer getActivatedUser() {
            return activatedCustomer;
    }

    public void setActivatedUser(Customer activatedCustomer) {
            this.activatedCustomer = activatedCustomer;
    }

    private ArrayList<Customer> customers = new ArrayList<Customer>();
    private Customer activatedCustomer;
}
